package com.ytjj.qmyx.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.ytjj.common.api.CommonPage;
import com.ytjj.common.api.CommonResult;
import com.ytjj.qmyx.admin.mapper.PlatformStrictInfoMapper;
import com.ytjj.qmyx.admin.model.PlatformStrictInfo;
import com.ytjj.qmyx.admin.model.request.PlatformStrictInfoRequest;
import com.ytjj.qmyx.admin.model.response.PlatformStrictInfoResponse;
import com.ytjj.qmyx.admin.service.PlatformStrictInfoService;
import com.ytjj.qmyx.users.mapper.UsersInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class PlatformSstrictInfoServiceImpl implements PlatformStrictInfoService {
    @Resource
    private PlatformStrictInfoMapper yxPlatformStrictInfoMapper;
    @Resource
    private UsersInfoMapper usersInfoMapper;

    @Override
    public CommonPage query(PlatformStrictInfoRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<PlatformStrictInfoResponse> list = yxPlatformStrictInfoMapper.queryList(request);
        list.stream().forEach(item -> {
            if (!StringUtils.isEmpty(item.getPollContent())){
                item.setPollContentList(Arrays.asList(item.getPollContent().split("\\|")));
            }
        });
        CommonPage page = CommonPage.restPage(list);
        return page;
    }

    @Override
    public CommonResult save(PlatformStrictInfo platformStrictInfo) {
        List<String> pollContentList = platformStrictInfo.getPollContentList();
        String pollContent = "";
        if (!CollectionUtils.isEmpty(pollContentList)) {
            for (int i = 0; i < pollContentList.size(); i++) {
                if (i < pollContentList.size() - 1) {
                    pollContent = pollContent + pollContentList.get(i) + ("|");
                } else {
                    pollContent = pollContent + pollContentList.get(i);
                }
            }
        }
        platformStrictInfo.setPollContent(pollContent);
        int result = 0;
        if (null == platformStrictInfo.getId()) {
            platformStrictInfo.setCreateTime(new Date());
            result = yxPlatformStrictInfoMapper.insert(platformStrictInfo);
        }else {
            result = yxPlatformStrictInfoMapper.updateByPrimaryKey(platformStrictInfo);
        }
        return result > 0 ? CommonResult.success(result) : CommonResult.failed();
    }

    @Override
    public PlatformStrictInfoResponse detail(Integer id) {
        PlatformStrictInfoResponse response = yxPlatformStrictInfoMapper.detailById(id);
        response.setPollContentList(Arrays.asList(response.getPollContent().split("\\|")));
        return null == response ? new PlatformStrictInfoResponse() : response;
    }
}
